/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogAtleti.java
 *
 * Created on 29-mar-2012, 18.03.56
 */
package it.nesimuge.score.gui;

import it.lapomuge.db.dao.DbManager;
import it.lapomuge.db.entity.Atleta;
import it.nesimuge.score.gui.listener.ListenerAtletiChanged;
import it.nesimuge.score.gui.model.ModelListAtleti;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Muge
 */
public class JDialogAtleti extends javax.swing.JDialog {
    
    private List<ListenerAtletiChanged> listener = new ArrayList<ListenerAtletiChanged>();

    /** Creates new form JDialogAtleti */
    public JDialogAtleti(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        fireAtletiCambiati();
        
    }
    
    public void addLinstener(ListenerAtletiChanged l) {
        this.listener.add(l);
    }
    
    public void fireAtletiCambiati() {
        ModelListAtleti model = (ModelListAtleti) this.jAtletiList.getModel();
        model.setAtleti(DbManager.getInstance().findAll(Atleta.class));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jAtletiList = new javax.swing.JList();
        jSeleziona = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jCognome = new javax.swing.JTextField();
        jSalva = new javax.swing.JButton();
        jChiudi = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jAtletiList.setModel(new ModelListAtleti());
        jAtletiList.setName("jAtletiList"); // NOI18N
        jScrollPane1.setViewportView(jAtletiList);

        
        jSeleziona.setText("Seleziona"); // NOI18N
        jSeleziona.setName("jSeleziona"); // NOI18N
        jSeleziona.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jSelezionaMouseClicked(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(""); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jNome.setText("Nome"); // NOI18N
        jNome.setName("jNome"); // NOI18N

        jLabel2.setText(""); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jCognome.setText("Cognome"); // NOI18N
        jCognome.setName("jCognome"); // NOI18N

        jSalva.setText("Salva"); // NOI18N
        jSalva.setName("jSalva"); // NOI18N
        jSalva.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jSalvaMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jNome, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                            .addComponent(jCognome, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)))
                    .addComponent(jSalva, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCognome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSalva)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jChiudi.setText("Chiudi"); // NOI18N
        jChiudi.setName("jChiudi"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                    .addComponent(jChiudi)
                    .addComponent(jSeleziona, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jSeleziona)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jChiudi)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jSalvaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSalvaMouseClicked
    Atleta a = new Atleta();
    a.setNome(this.jNome.getText());
    a.setCognome(this.jCognome.getText());
    DbManager.getInstance().save(Atleta.class, a);
    fireAtletiCambiati();
    
}//GEN-LAST:event_jSalvaMouseClicked
    
private void jSelezionaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSelezionaMouseClicked
    int[] selectedIndices = jAtletiList.getSelectedIndices();
    if (selectedIndices != null && selectedIndices.length >= 0) {
        Atleta[] atleti = new Atleta[selectedIndices.length];
        ModelListAtleti model = (ModelListAtleti) this.jAtletiList.getModel();
        
        for (int i = 0; i < atleti.length; i++) {
            atleti[i] = model.getAtleti()[selectedIndices[i]];
        }
        
        for (ListenerAtletiChanged l : this.listener) {
            l.fireSelezioneCambiata(this,atleti);
        }
    }
}//GEN-LAST:event_jSelezionaMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JDialogAtleti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JDialogAtleti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JDialogAtleti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JDialogAtleti.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                JDialogAtleti dialog = new JDialogAtleti(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jAtletiList;
    private javax.swing.JButton jChiudi;
    private javax.swing.JTextField jCognome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jNome;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jSalva;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jSeleziona;
    // End of variables declaration//GEN-END:variables
}
